package com.hnkywl.sts.entity.zjgl;

import com.hnkywl.sts.entity.BaseEntity;
import com.hnkywl.sts.entity.base.Sjzdz;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.sfgl.Sfk;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * 说明：保证金预警
 * 包:        com.hnkywl.sts.entity.zjgl
 * 类描述:    信用证
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Entity
@Table(name = "T_BIZ_BZJYJ")
@Where(clause = "deleted!='1'")
public class Bzjyj extends BaseEntity {
    @Id
    @SequenceGenerator(name = "seqBzjyj", allocationSize = 1, sequenceName = "SEQ_BIZ_BZJYJ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqBzjyj")
    private Long id;

    /**
     * 采购合同
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CGHT_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private Cght cght;

    /**
     * 当前交易日平均指数
     */
    @Formula("(select nvl(jgzs.pjzs,0) from t_biz_jgzs jgzs where  jgzs.deleted = 0 and jgzs.jgrq =  trunc(sysdate))")
    private Double dqjyzs;

    /**
     * 投资付款日平均指数
     */
    @Formula("(select nvl(jgzs.pjzs,0) from t_biz_jgzs jgzs where jgzs.deleted = 0 and jgzs.jgrq = (select ht.fksj from t_biz_ht ht where ht.id = CGHT_ID))")
    private Double tzjyzs;

    /**
     * 普钢指数比例 = （当前交易日平均指数-投资付款日平均指数）/当前交易日平均指数
     */
    @Formula("(select ((select nvl(jgzs.pjzs,0) from t_biz_jgzs jgzs where jgzs.jgrq =  trunc(sysdate) and jgzs.deleted = 0) - (select nvl(jgzs.pjzs,0) from t_biz_jgzs jgzs where  jgzs.deleted = 0 and jgzs.jgrq = (select ht.fksj from t_biz_ht ht where ht.id = CGHT_ID)))/(select nvl(jgzs.pjzs,0) from t_biz_jgzs jgzs where jgzs.deleted = 0 and jgzs.jgrq = (select ht.fksj from t_biz_ht ht where ht.id = CGHT_ID)) from dual)")
    private Double pgzsbl;

    /**
     * 保证金比例
     */
    @Formula("( select nvl(bzj.bzjbl,0) from t_biz_bzj bzj where bzj.deleted = 0 and  bzj.ht_id = CGHT_ID and bzj.status = 1 and rownum = 1)")
    private Double bzjbl;

    /**
     * 所有的锁定这个采购合同的收款保证金总数
     */
    @Formula("(select sum(sfk.ye) from t_biz_bzj bzj,t_biz_sfk sfk where bzj.sk_id = sfk.id and bzj.deleted = 0 and sfk.deleted = 0 and bzj.ht_id = CGHT_ID and bzj.status = 1)")
    private Double bzjje;

    /**
     * 采购合同的未赎货的总金额
     */
    @Formula("(select sum(nvl(kh.sl * cgdmx.hsj,0)) from t_biz_kh kh,t_biz_cgdmx cgdmx where kh.cgdmx_id = cgdmx.id and cgdmx.deleted = 0 and  kh.deleted = 0 and kh.cgd_id in (select cgd.id from t_biz_cgd cgd where cgd.deleted = 0 and cgd.cght_id = CGHT_ID) and kh.sl > 0)")
    private Double wshzje;

    /**
     * 当前实际保证金比例（C）=保证金金额/采购合同中没有赎货的库存金额
     */
    @Formula("(select ((select sum(sfk.ye) from t_biz_bzj bzj,t_biz_sfk sfk where bzj.sk_id = sfk.id and bzj.deleted = 0 and sfk.deleted = 0 and bzj.ht_id = CGHT_ID and bzj.status = 1)/(select sum(nvl(kh.sl * cgdmx.hsj,0)) from t_biz_kh kh,t_biz_cgdmx cgdmx where kh.cgdmx_id = cgdmx.id and cgdmx.deleted = 0 and  kh.deleted = 0 and kh.cgd_id in (select cgd.id from t_biz_cgd cgd where cgd.deleted = 0 and cgd.cght_id = CGHT_ID) and kh.sl > 0)) from dual)")
    private Double dqbzjbl;

    /**
     * 状态
     */
    @Formula("(select '正常' from dual)")
    private String zt;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Cght getCght() {
        return cght;
    }

    public void setCght(Cght cght) {
        this.cght = cght;
    }

    public Double getDqjyzs() {
        return dqjyzs;
    }

    public void setDqjyzs(Double dqjyzs) {
        this.dqjyzs = dqjyzs;
    }

    public Double getTzjyzs() {
        return tzjyzs;
    }

    public void setTzjyzs(Double tzjyzs) {
        this.tzjyzs = tzjyzs;
    }

    public Double getPgzsbl() {
        return pgzsbl;
    }

    public void setPgzsbl(Double pgzsbl) {
        this.pgzsbl = pgzsbl;
    }

    public Double getBzjbl() {
        return bzjbl;
    }

    public void setBzjbl(Double bzjbl) {
        this.bzjbl = bzjbl;
    }

    public Double getBzjje() {
        return bzjje;
    }

    public void setBzjje(Double bzjje) {
        this.bzjje = bzjje;
    }

    public Double getWshzje() {
        return wshzje;
    }

    public void setWshzje(Double wshzje) {
        this.wshzje = wshzje;
    }

    public Double getDqbzjbl() {
        return dqbzjbl;
    }

    public void setDqbzjbl(Double dqbzjbl) {
        this.dqbzjbl = dqbzjbl;
    }

    public String getZt() {
        DecimalFormat df = new DecimalFormat("0.00");
        String z = "正常";
        if (this.pgzsbl != null) {
            if (this.pgzsbl < 0) {
                String p = df.format(Math.abs(this.pgzsbl));
                Double p1 = Double.valueOf(p);
                if (p1 > 0.05) {
                    z = "追加保证金或者提货";
                }
            }
            if (this.dqbzjbl != null && this.pgzsbl + this.dqbzjbl + 0.02 <= this.bzjbl / 100) {
                z = "追加保证金或者提货";
            }
        }
        return z;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }
}
